/*!
 *	@file		w25qxx.c
 *	@author		T4056 Student
 *	@date		2024-07-13
 *	@version	V1.0
 *	@par		CopyRight(c)
 *				2024 T4056 Student.All rights reserved.
 */
#include "w25qxx.h"

static void w25qxx_set_cs(uint8_t level);
static uint8_t w25qxx_write_read_byte(uint8_t tx_byte);

static void w25qxx_set_cs(uint8_t level)
{
    HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,level ? GPIO_PIN_SET : GPIO_PIN_RESET);
}

static uint8_t w25qxx_write_read_byte(uint8_t tx_byte)
{
	uint8_t rx_byte = 0;
    HAL_SPI_TransmitReceive(&hspi1,&tx_byte,&rx_byte,1,100);
    return rx_byte;
}

/*!
 *  @details 1.Enable Flash CS Pin 
             2.Send SPI Command 0x90
             3.Send 3 dummy Command 0x00
             4.Read 2 Byte as id
             5.Disable Flash CE Pin
             6.Return id
    @bug    If there are any problems with SPI Transmit that have not been resolved
 */
uint16_t w25qxx_read_id(void)
{
	uint16_t id = 0;
    w25qxx_set_cs(0);
    w25qxx_write_read_byte(0x90);
    w25qxx_write_read_byte(0x00);
    w25qxx_write_read_byte(0x00);
    w25qxx_write_read_byte(0x00);
    id = w25qxx_write_read_byte(0xff);
    id <<= 8;
    id |= w25qxx_write_read_byte(0xff);
    w25qxx_set_cs(1);
    return id;
}
